{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MLP MNIST\n",
    "MNIST数据集请点击[这里查看](http://yann.lecun.com/exdb/mnist/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout\n",
    "from keras.optimizers import RMSprop\n",
    "\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 128\n",
    "num_classes = 10\n",
    "epochs = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the data, split between train and test sets\n",
    "(x_train, y_train), (x_test, y_test) = mnist.load_data(already_path='data/mnist.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000 train samples\n",
      "10000 test samples\n"
     ]
    }
   ],
   "source": [
    "x_train = x_train.reshape(60000, 784)\n",
    "x_test = x_test.reshape(10000, 784)\n",
    "x_train = x_train.astype('float32')\n",
    "x_test = x_test.astype('float32')\n",
    "x_train /= 255\n",
    "x_test /= 255\n",
    "print(x_train.shape[0], 'train samples')\n",
    "print(x_test.shape[0], 'test samples')\n",
    "\n",
    "# convert class vectors to binary class matrices\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_1 (Dense)              (None, 512)               401920    \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 512)               262656    \n",
      "_________________________________________________________________\n",
      "dropout_2 (Dropout)          (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 10)                5130      \n",
      "=================================================================\n",
      "Total params: 669,706\n",
      "Trainable params: 669,706\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0207 15:10:09.495326  3592 deprecation_wrapper.py:119] From D:\\dev_tools\\anaconda\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/20\n",
      "60000/60000 [==============================] - 10s 165us/step - loss: 0.2461 - accuracy: 0.9240 - val_loss: 0.1257 - val_accuracy: 0.9610\n",
      "Epoch 2/20\n",
      "60000/60000 [==============================] - 9s 153us/step - loss: 0.1033 - accuracy: 0.9681 - val_loss: 0.0977 - val_accuracy: 0.9705\n",
      "Epoch 3/20\n",
      "60000/60000 [==============================] - 9s 150us/step - loss: 0.0754 - accuracy: 0.9781 - val_loss: 0.0781 - val_accuracy: 0.9773\n",
      "Epoch 4/20\n",
      "60000/60000 [==============================] - 9s 149us/step - loss: 0.0608 - accuracy: 0.9817 - val_loss: 0.0710 - val_accuracy: 0.9796\n",
      "Epoch 5/20\n",
      "60000/60000 [==============================] - 9s 148us/step - loss: 0.0512 - accuracy: 0.9853 - val_loss: 0.0733 - val_accuracy: 0.9807\n",
      "Epoch 6/20\n",
      "60000/60000 [==============================] - 9s 145us/step - loss: 0.0448 - accuracy: 0.9869 - val_loss: 0.0802 - val_accuracy: 0.9820\n",
      "Epoch 7/20\n",
      "60000/60000 [==============================] - 9s 147us/step - loss: 0.0402 - accuracy: 0.9878 - val_loss: 0.0894 - val_accuracy: 0.9812\n",
      "Epoch 8/20\n",
      "60000/60000 [==============================] - 9s 147us/step - loss: 0.0342 - accuracy: 0.9898 - val_loss: 0.0980 - val_accuracy: 0.9792\n",
      "Epoch 9/20\n",
      "60000/60000 [==============================] - 10s 169us/step - loss: 0.0312 - accuracy: 0.9907 - val_loss: 0.0850 - val_accuracy: 0.9822\n",
      "Epoch 10/20\n",
      "60000/60000 [==============================] - 9s 152us/step - loss: 0.0267 - accuracy: 0.9920 - val_loss: 0.0890 - val_accuracy: 0.9830\n",
      "Epoch 11/20\n",
      "60000/60000 [==============================] - 9s 144us/step - loss: 0.0271 - accuracy: 0.9923 - val_loss: 0.0917 - val_accuracy: 0.9829\n",
      "Epoch 12/20\n",
      "60000/60000 [==============================] - 9s 147us/step - loss: 0.0232 - accuracy: 0.9929 - val_loss: 0.0965 - val_accuracy: 0.9834\n",
      "Epoch 13/20\n",
      "60000/60000 [==============================] - 9s 152us/step - loss: 0.0224 - accuracy: 0.9935 - val_loss: 0.1020 - val_accuracy: 0.9836\n",
      "Epoch 14/20\n",
      "60000/60000 [==============================] - 9s 145us/step - loss: 0.0198 - accuracy: 0.9941 - val_loss: 0.1166 - val_accuracy: 0.9831\n",
      "Epoch 15/20\n",
      "60000/60000 [==============================] - 9s 145us/step - loss: 0.0229 - accuracy: 0.9938 - val_loss: 0.1086 - val_accuracy: 0.9838\n",
      "Epoch 16/20\n",
      "60000/60000 [==============================] - 9s 145us/step - loss: 0.0214 - accuracy: 0.9944 - val_loss: 0.1062 - val_accuracy: 0.9842\n",
      "Epoch 17/20\n",
      "60000/60000 [==============================] - 9s 145us/step - loss: 0.0205 - accuracy: 0.9944 - val_loss: 0.1222 - val_accuracy: 0.9808\n",
      "Epoch 18/20\n",
      "60000/60000 [==============================] - 9s 156us/step - loss: 0.0183 - accuracy: 0.9953 - val_loss: 0.1294 - val_accuracy: 0.9826\n",
      "Epoch 19/20\n",
      "60000/60000 [==============================] - 9s 149us/step - loss: 0.0184 - accuracy: 0.9953 - val_loss: 0.1213 - val_accuracy: 0.9829\n",
      "Epoch 20/20\n",
      "60000/60000 [==============================] - 9s 143us/step - loss: 0.0151 - accuracy: 0.9956 - val_loss: 0.1393 - val_accuracy: 0.9835\n",
      "Test loss: 0.1393048581601988\n",
      "Test accuracy: 0.9835000038146973\n"
     ]
    }
   ],
   "source": [
    "model = Sequential()\n",
    "model.add(Dense(512, activation='relu', input_shape=(784,)))\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Dense(512, activation='relu'))\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Dense(num_classes, activation='softmax'))\n",
    "\n",
    "model.summary()\n",
    "\n",
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=RMSprop(),\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "start_time = time.time()\n",
    "history = model.fit(x_train, y_train,\n",
    "                    batch_size=batch_size,\n",
    "                    epochs=epochs,\n",
    "                    verbose=1,\n",
    "                    validation_data=(x_test, y_test))\n",
    "end_time = time.time()\n",
    "score = model.evaluate(x_test, y_test, verbose=0)\n",
    "print('Test loss:', score[0])\n",
    "print('Test accuracy:', score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
